﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using TaiwuZD;

namespace Test
{
    public class TwSkill : TwObject
    {





        public override object Clone()
        {
            throw new NotImplementedException();
        }
    }


    class Program
    {
        static void Main(string[] args)
        {

            int[] idd = new int[] { 0, 99, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 1001 };


            Dictionary<int, TwSkill> dic = new Dictionary<int, TwSkill>();

            using (var sr= new StreamReader("1.txt"))
            {
                sr.ReadLine();
                while (!sr.EndOfStream)
                {
                    var str = sr.ReadLine().Split(',');

                    TwSkill twSkill = new TwSkill();

                    twSkill.SetData(999, int.Parse(str[0]));
                    for (int i = 1; i < str.Length; i++)
                    {
                        twSkill.SetData(idd[i-1], str[i]);
                    }

                    dic.Add(int.Parse(str[0]), twSkill);
                }
            }

            using (var sw = new StreamWriter("TwSkill.json"))
            {
                var str = JsonConvert.SerializeObject(dic);
                sw.Write(str);
                sw.Flush();
            }




            return;

            var dir = @"D:\Program Files (x86)\Steam\steamapps\common\The Scroll Of Taiwu\The Scroll Of Taiwu Alpha V1.0_Data\SaveFiles\Date_1";

            for (int i = 1; i < 10; i++)
            {
                var file = $"{dir}\\TW_Save_Date_{i}.tw";
                DDD(file);
                Console.WriteLine($"L{i}");
            }
        }


        public static void DDD(string fileName)
        {
            StreamReader streamReader = File.OpenText(fileName);
            string pString = streamReader.ReadToEnd();
            pString = RijndaelDecrypt(pString, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            streamReader.Close();

            using (var sw = new StreamWriter(fileName+".json"))
            {
                sw.Write(pString);
                sw.Flush();
            }

        }

        private static string RijndaelDecrypt(string pString, string pKey)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(pKey);
            byte[] array = Convert.FromBase64String(pString);
            byte[] bytes2 = new RijndaelManaged
            {
                Key = bytes,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            }.CreateDecryptor().TransformFinalBlock(array, 0, array.Length);
            return Encoding.UTF8.GetString(bytes2);
        }
    }
}
